package org.nViron.nvRender;

/**
 * Three dimensional vector class
 * @author Roland Haring <br>
 * TODO implement this class native <p>
 * <script type="text/javascript" src="http://www.ohloh.net/p/376434/widgets/project_basic_stats.js"></script>
 */
public class nvVec3 {
    public float x, y, z;

    public nvVec3() {}

    public nvVec3(float x, float y, float z) {
        set(x, y, z);
    }

    public nvVec3(nvVec3 v) {
        set(v.x, v.y, v.z);
    }

    public nvVec3 add(float f) {
        nvVec3 ret = new nvVec3();

        ret.x = x + f;
        ret.y = y + f;
        ret.z = z + f;

        return ret;
    }

    public nvVec3 add(nvVec3 v) {
        nvVec3 ret = new nvVec3();

        ret.x = x + v.x;
        ret.y = y + v.y;
        ret.z = z + v.z;

        return ret;
    }

    public void assign(nvVec3 other) {
        set(other.x, other.y, other.z);
    }

    public void copy(nvVec3 v) {
        x = v.x;
        y = v.y;
        z = v.z;
    }

    public nvVec3 cross(nvVec3 v) {
        nvVec3 ret = new nvVec3();

        ret.x = y * v.z - z * v.y;
        ret.y = z * v.x - x * v.z;
        ret.z = x * v.y - y * v.x;

        return ret;
    }

    public float distance(nvVec3 cp) {
        return (float) Math.sqrt(distanceSquare(cp));
    }

    public float distanceSquare(nvVec3 cp) {
        return (x - cp.x) * (x - cp.x) + (y - cp.y) * (y - cp.y) + (z - cp.z) * (z - cp.z);
    }

    public nvVec3 div(float f) {
        nvVec3 ret = new nvVec3();

        ret.x = x / f;
        ret.y = y / f;
        ret.z = z / f;

        return ret;
    }

    public nvVec3 div(nvVec3 v) {
        nvVec3 ret = new nvVec3();

        ret.x = x / v.x;
        ret.y = y / v.y;
        ret.z = z / v.z;

        return ret;
    }

    // inner, scalar, dot product
    public float dot(nvVec3 v) {
        return (x * v.x + y * v.y + z * v.z);
    }

    public nvVec3 inv() {
        nvVec3 ret = new nvVec3();

        ret.x = -x;
        ret.y = -y;
        ret.z = -z;

        return ret;
    }

    public float length() {
        return ((float) Math.sqrt(x * x + y * y + z * z));
    }

    public nvVec3 mul(float f) {
        nvVec3 ret = new nvVec3();

        ret.x = x * f;
        ret.y = y * f;
        ret.z = z * f;

        return ret;
    }

    public nvVec3 mul(nvVec3 v) {
        nvVec3 ret = new nvVec3();

        ret.x = x * v.x;
        ret.y = y * v.y;
        ret.z = z * v.z;

        return ret;
    }

    public void normalize() {
        float len;

        len = length();

        if (len != 0) {
            x /= len;
            y /= len;
            z /= len;
        }
    }

    public void set(float _x, float _y, float _z) {
        x = _x;
        y = _y;
        z = _z;
    }

    public nvVec3 sub(float f) {
        nvVec3 ret = new nvVec3();

        ret.x = x - f;
        ret.y = y - f;
        ret.z = z - f;

        return ret;
    }

    public nvVec3 sub(nvVec3 v) {
        nvVec3 ret = new nvVec3();

        ret.x = x - v.x;
        ret.y = y - v.y;
        ret.z = z - v.z;

        return ret;
    }
}



